package com.jzdjt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.ChargeEntity;
import com.jzdjt.entity.VideoEntity;
import com.jzdjt.tool.DBUtil; 

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetChargeListServlet extends HttpServlet {

    public GetChargeListServlet() {
        super();
    }

    @Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	 response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
         boolean b=false;
         //根据标示名获取JSP文件中表单所包含的参数
         String stuid=request.getParameter("stuid"); 
         
         String  result = getChargeList(stuid);  
         
         
         PrintWriter out = response.getWriter();//回应请求
         
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result); 
    }

    @Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	// doGet(request, response); 
         //设置客户端的解码方式为utf-8
         response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
        
         //根据标示名获取JSP文件中表单所包含的参数
         String stuid=request.getParameter("stuid"); 
         
         String  result =  getChargeList(stuid);   
         
         
         PrintWriter out = response.getWriter();//回应请求
          
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result); 
    }

	private String getChargeList(String stuid) {
		try {
			JSONObject jsonObject = new JSONObject();
			String sql = "select * from recharge where studentID='"+stuid+"'";
			System.out.println("getChargeList:"+sql);
	        DBUtil dbUtil = new DBUtil();
	        List<ChargeEntity> chargelist = dbUtil.QueryFromRecharge(sql);
	        JSONArray array =  JSONArray.fromObject(chargelist);
	        System.out.println("getChargeList:"+array.toString());
	        return array.toString();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "";
		}
		
	}

}